<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/27
 * Time: 22:17
 */


//获取token
$url ="https://api.weixin.qq.com/cgi-bin/token?";
$grant_type = "client_credential";
$appid = "wx94010b1eb2f5362d";
$secret = "51ac1f02b2cc1c5a643c85f908acb680";
$url = $url."grant_type=".$grant_type."&appid=".$appid."&secret=".$secret;

$str = file_get_contents($url);
$jsonStr = json_decode($str);


if(!isset($jsonStr->access_token))
{
    echo 0;//未获取到token
    exit;
}
    echo 1;//获取access_token成功
    $access_token = $jsonStr->access_token;//应该写入session
    //进行下一步的接口条用

    //用户列表
    //$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token
    //var_dump(file_get_contents($url));

    //用户详细信息
    //$urlUserInfo = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token;
    //var_dump(file_get_contents($urlUserInfo));


    //自定义菜单
    $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
    $data='{
    "button": [
        {
            "type": "click", 
            "name": "今日66", 
            "key": "V1001_TODAY_MUSIC"
        }, 
        {
            "name": "菜单", 
            "sub_button": [
                {
                    "type": "view", 
                    "name": "搜66", 
                    "url": "http://www.soso.com/"
                }, 
                {
                    "type": "click", 
                    "name": "赞一下2", 
                    "key": "V1001_GOOD"
                }
            ]
        }
    ]
}';

post_ex($url,$data);





//请求接口方法
function postcurl($url,$data = null){//第二个post方法
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return  $output=json_decode($output,true);
}

function post_ex($url,$data)
{//第一个post方法
    //$data = http_build_query($data);
    $opts = array (
        'http' => array (
    'method' => 'POST',
    'header'=> "Content-type: application/x-www-form-urlencodedrn",
    "Content-Length: " . strlen($data) . "rn",
    'content' => $data
    )
);

$context = stream_context_create($opts);
$html = file_get_contents($url, false, $context);
var_dump($html);
file_put_contents("1.txt",print_r(['sdf','ewrw'],true));

}
